﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类DocumentNew。
    /// </summary>
    public class DocumentNew
    {
        public DocumentNew()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid DocumentId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Test");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DocumentNew where DocumentId=@DocumentId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DocumentId", DbType.Guid, DocumentId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.DocumentNew model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DocumentNew(");
            strSql.Append("DocumentId,DocumentType,Title,Body,AttachmentName,AttachmentFileName,AttachmentBody,AttachmentType,AttachmentSize,PubDate,CreateDate,PubUserId,PubUserNo,DeleteFlag,RcvDepartment,CategoryId,IsPublic,IsEveryone,IsNewWindow,SendDepartmentId,CityId)");

            strSql.Append(" values (");
            strSql.Append("@DocumentId,@DocumentType,@Title,@Body,@AttachmentName,@AttachmentFileName,@AttachmentBody,@AttachmentType,@AttachmentSize,@PubDate,@CreateDate,@PubUserId,@PubUserNo,@DeleteFlag,@RcvDepartment,@CategoryId,@IsPublic,@IsEveryone,@IsNewWindow,@SendDepartmentId,@CityId)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Test");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DocumentId", DbType.Guid, model.DocumentId);
            db.AddInParameter(dbCommand, "DocumentType", DbType.Int32, model.DocumentType);
            db.AddInParameter(dbCommand, "Title", DbType.AnsiString, model.Title);
            db.AddInParameter(dbCommand, "Body", DbType.AnsiString, model.Body);
            db.AddInParameter(dbCommand, "AttachmentName", DbType.AnsiString, model.AttachmentName);
            db.AddInParameter(dbCommand, "AttachmentFileName", DbType.AnsiString, model.AttachmentFileName);
            db.AddInParameter(dbCommand, "AttachmentBody", DbType.Binary, model.AttachmentBody);
            db.AddInParameter(dbCommand, "AttachmentType", DbType.AnsiString, model.AttachmentType);
            db.AddInParameter(dbCommand, "AttachmentSize", DbType.Int32, model.AttachmentSize);
            db.AddInParameter(dbCommand, "PubDate", DbType.DateTime, model.PubDate);
            db.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, model.CreateDate);
            db.AddInParameter(dbCommand, "PubUserId", DbType.Guid, model.PubUserId);
            db.AddInParameter(dbCommand, "PubUserNo", DbType.AnsiString, model.PubUserNo);
            db.AddInParameter(dbCommand, "DeleteFlag", DbType.Boolean, model.DeleteFlag);
            db.AddInParameter(dbCommand, "RcvDepartment", DbType.AnsiString, model.RcvDepartment);
            db.AddInParameter(dbCommand, "CategoryId", DbType.AnsiString, model.CategoryId);
            db.AddInParameter(dbCommand, "IsPublic", DbType.Boolean, model.IsPublic);
            db.AddInParameter(dbCommand, "IsEveryone", DbType.Boolean, model.IsEveryone);
            db.AddInParameter(dbCommand, "IsNewWindow", DbType.Boolean, model.IsNewWindow);
            db.AddInParameter(dbCommand, "SendDepartmentId", DbType.Guid, model.SendDepartmentId);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.DocumentNew model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DocumentNew set ");
            strSql.Append("DocumentType=@DocumentType,");
            strSql.Append("Title=@Title,");
            strSql.Append("Body=@Body,");
            strSql.Append("AttachmentName=@AttachmentName,");
            strSql.Append("AttachmentFileName=@AttachmentFileName,");
            strSql.Append("AttachmentBody=@AttachmentBody,");
            strSql.Append("AttachmentType=@AttachmentType,");
            strSql.Append("AttachmentSize=@AttachmentSize,");
            strSql.Append("PubDate=@PubDate,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("PubUserId=@PubUserId,");
            strSql.Append("PubUserNo=@PubUserNo,");
            strSql.Append("DeleteFlag=@DeleteFlag,");
            strSql.Append("RcvDepartment=@RcvDepartment,");
            strSql.Append("CategoryId=@CategoryId,");
            strSql.Append("IsPublic=@IsPublic,");
            strSql.Append("IsEveryone=@IsEveryone,");
            strSql.Append("IsNewWindow=@IsNewWindow,");
            strSql.Append("SendDepartmentId=@SendDepartmentId,");
            strSql.Append("CityId=@CityId");
            strSql.Append(" where DocumentId=@DocumentId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Test");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DocumentId", DbType.Guid, model.DocumentId);
            db.AddInParameter(dbCommand, "DocumentType", DbType.Int32, model.DocumentType);
            db.AddInParameter(dbCommand, "Title", DbType.AnsiString, model.Title);
            db.AddInParameter(dbCommand, "Body", DbType.AnsiString, model.Body);
            db.AddInParameter(dbCommand, "AttachmentName", DbType.AnsiString, model.AttachmentName);
            db.AddInParameter(dbCommand, "AttachmentFileName", DbType.AnsiString, model.AttachmentFileName);
            db.AddInParameter(dbCommand, "AttachmentBody", DbType.Binary, model.AttachmentBody);
            db.AddInParameter(dbCommand, "AttachmentType", DbType.AnsiString, model.AttachmentType);
            db.AddInParameter(dbCommand, "AttachmentSize", DbType.Int32, model.AttachmentSize);
            db.AddInParameter(dbCommand, "PubDate", DbType.DateTime, model.PubDate);
            db.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, model.CreateDate);
            db.AddInParameter(dbCommand, "PubUserId", DbType.Guid, model.PubUserId);
            db.AddInParameter(dbCommand, "PubUserNo", DbType.AnsiString, model.PubUserNo);
            db.AddInParameter(dbCommand, "DeleteFlag", DbType.Boolean, model.DeleteFlag);
            db.AddInParameter(dbCommand, "RcvDepartment", DbType.AnsiString, model.RcvDepartment);
            db.AddInParameter(dbCommand, "CategoryId", DbType.AnsiString, model.CategoryId);
            db.AddInParameter(dbCommand, "IsPublic", DbType.Boolean, model.IsPublic);
            db.AddInParameter(dbCommand, "IsEveryone", DbType.Boolean, model.IsEveryone);
            db.AddInParameter(dbCommand, "IsNewWindow", DbType.Boolean, model.IsNewWindow);
            db.AddInParameter(dbCommand, "SendDepartmentId", DbType.Guid, model.SendDepartmentId);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid DocumentId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DocumentNew ");
            strSql.Append(" where DocumentId=@DocumentId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Test");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DocumentId", DbType.Guid, DocumentId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.DocumentNew GetModel(Guid DocumentId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DocumentId,DocumentType,Title,Body,AttachmentName,AttachmentFileName,AttachmentBody,AttachmentType,AttachmentSize,PubDate,CreateDate,PubUserId,PubUserNo,DeleteFlag,RcvDepartment,CategoryId,IsPublic,IsEveryone,IsNewWindow,SendDepartmentId,CityId from DocumentNew ");
            strSql.Append(" where DocumentId=@DocumentId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Test");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DocumentId", DbType.Guid, DocumentId);
            Chenxin.Lib.House.Entity.DocumentNew model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DocumentId,DocumentType,Title,Body,AttachmentName,AttachmentFileName,AttachmentBody,AttachmentType,AttachmentSize,PubDate,CreateDate,PubUserId,PubUserNo,DeleteFlag,RcvDepartment,CategoryId,IsPublic,IsEveryone,IsNewWindow,SendDepartmentId,CityId ");
            strSql.Append(" FROM DocumentNew ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Test");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetReportList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 4 DocumentId,DocumentType,Title,Body,AttachmentName,AttachmentFileName,AttachmentBody,AttachmentType,AttachmentSize,PubDate,CreateDate,PubUserId,PubUserNo,DeleteFlag,RcvDepartment,CategoryId,IsPublic,IsEveryone,IsNewWindow,SendDepartmentId,CityId ");
            strSql.Append(" FROM DocumentNew ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Test");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Test");
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "DocumentNew");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.DocumentNew> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DocumentId,DocumentType,Title,Body,AttachmentName,AttachmentFileName,AttachmentBody,AttachmentType,AttachmentSize,PubDate,CreateDate,PubUserId,PubUserNo,DeleteFlag,RcvDepartment,CategoryId,IsPublic,IsEveryone,IsNewWindow,SendDepartmentId,CityId ");
            strSql.Append(" FROM DocumentNew ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.DocumentNew> list = new List<Chenxin.Lib.House.Entity.DocumentNew>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Test");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.DocumentNew ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.DocumentNew model = new Chenxin.Lib.House.Entity.DocumentNew();
            object ojb;
            ojb = dataReader["DocumentId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DocumentId = new Guid(ojb.ToString());
            }
            ojb = dataReader["DocumentType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DocumentType = (int)ojb;
            }
            model.Title = dataReader["Title"].ToString();
            model.Body = dataReader["Body"].ToString();
            model.AttachmentName = dataReader["AttachmentName"].ToString();
            model.AttachmentFileName = dataReader["AttachmentFileName"].ToString();
            ojb = dataReader["AttachmentBody"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AttachmentBody = (byte[])ojb;
            }
            model.AttachmentType = dataReader["AttachmentType"].ToString();
            ojb = dataReader["AttachmentSize"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AttachmentSize = (int)ojb;
            }
            ojb = dataReader["PubDate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PubDate = (DateTime)ojb;
            }
            ojb = dataReader["CreateDate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CreateDate = (DateTime)ojb;
            }
            ojb = dataReader["PubUserId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PubUserId = new Guid(ojb.ToString());
            }
            model.PubUserNo = dataReader["PubUserNo"].ToString();
            ojb = dataReader["DeleteFlag"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DeleteFlag = (bool)ojb;
            }
            model.RcvDepartment = dataReader["RcvDepartment"].ToString();
            model.CategoryId = dataReader["CategoryId"].ToString();
            ojb = dataReader["IsPublic"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsPublic = (bool)ojb;
            }
            ojb = dataReader["IsEveryone"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsEveryone = (bool)ojb;
            }
            ojb = dataReader["IsNewWindow"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsNewWindow = (bool)ojb;
            }
            ojb = dataReader["SendDepartmentId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SendDepartmentId = new Guid(ojb.ToString());
            }
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            return model;
        }

        #endregion  成员方法
    }
}

